package com.qlylkj.lbk.ui.splash

import android.Manifest
import android.content.Intent
import androidx.lifecycle.Observer
import com.forest.core.etx.startKtxActivity
import com.forest.core.etx.toast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.PreferenceHelper
import com.qlylkj.lbk.base.BaseVMActivity
import com.qlylkj.lbk.model.event.LiveEvent
import com.qlylkj.lbk.ui.main.MainActivity
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

class SplashActivity : BaseVMActivity<SplashViewModel>() {

    companion object {

        val REQUIRE_PERMISSION = arrayOf(
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )

        const val RC_REQUIRE_PERMISSION_PERM = 1001

    }

    override fun providerVMClass() = SplashViewModel::class.java

    override fun setLayoutId() = R.layout.activity_splash

    override fun initView() {
        mImmersionBar.fitsSystemWindows(true).transparentStatusBar().statusBarDarkFont(true, 0.2f)
            .init()
    }

    override fun initData() {
        methodRequiresTwoPermission()
    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@SplashActivity, Observer { uiModel ->
                uiModel?.run {
                    if (getUserInfoSuccess) {
                        LiveEventBus.get(LiveEvent.LOGIN_KEY).post(true)
                    } else {
                        PreferenceHelper.logout()
                        LiveEventBus.get(LiveEvent.LOGIN_KEY).post(false)
                    }
                    startKtxActivity<MainActivity>()
                    finish()
                }
            })
        }
    }

    @AfterPermissionGranted(RC_REQUIRE_PERMISSION_PERM)
    private fun methodRequiresTwoPermission() {
        if (EasyPermissions.hasPermissions(this, *REQUIRE_PERMISSION)) {
            mViewModel.getUserInfo()
        } else {
            EasyPermissions.requestPermissions(
                this@SplashActivity,
                getString(R.string.prompt_read_phone_and_external_storage_state),
                RC_REQUIRE_PERMISSION_PERM,
                *REQUIRE_PERMISSION
            )
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    fun onPermissionsGranted(requestCode: Int, list: List<String>) {

    }

    fun onPermissionsDenied(requestCode: Int, list: List<String>) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, list)) {
            AppSettingsDialog.Builder(this).setTitle("缺少应用权限").setRationale(
                getString(
                    R.string.returned_from_app_settings_to_activity,
                    if (EasyPermissions.hasPermissions(this, *REQUIRE_PERMISSION)) "是" else "否"
                )
            ).build().show()
        } else {
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            toast(
                getString(
                    R.string.returned_from_app_settings_to_activity,
                    if (EasyPermissions.hasPermissions(this, *REQUIRE_PERMISSION)) "是" else "否"
                )
            )
            if (EasyPermissions.hasPermissions(this, *REQUIRE_PERMISSION)) {
                methodRequiresTwoPermission()
            }
        }
    }

}